package com.iceengine.j3d.particles;

/**
 * Ice Engine particle class for the particle engine.
 * @author penguin
 */
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.Color;
import org.lwjgl.opengl.GL11;
public class Particle {
    private Vector3f position;
    private Vector3f velocity;
    private Vector3f Gravitation;
    private Color myColor;
    private int lifetime;
    public long startTime;
    private boolean alive;

    /**
     * Particle constructor.
     * @param Lifetime Lifetime for the particle.
     * @param gravity Gravity for calculating positions.
     * @param Position Startposition.
     * @param Velocity Speed of the particles movement.
     * @param color Particle color.
     */
    public Particle(int Lifetime,Vector3f gravity, Vector3f Position, Vector3f Velocity, Color color)
    {
        Gravitation = gravity;
        position = Position;
        lifetime = Lifetime;
        myColor = color;
        velocity = Velocity;

        startTime = System.currentTimeMillis();

        alive = true;
    }
    /**
     * Updates the particles position.
     */
    public void Update()
    {
        if(alive)
        {
            if((System.currentTimeMillis() - startTime) <= lifetime)
            {
                position.x += velocity.x / 250;
                position.y += velocity.y / 250;
                position.z += velocity.z / 250;

                velocity.x *= 0.975f;
                velocity.y *= 0.975f;
                velocity.z *= 0.975f;

                velocity.x += Gravitation.x;
                velocity.y += Gravitation.y;
                velocity.z += Gravitation.z;
            } else {
                alive = false;
            }
        }
    }
    /**
     * Draws the particle to the screen.
     */
    public void drawParticle()
    {
        if (alive)
        {
            GL11.glColor4f(myColor.getRed(), myColor.getGreen(), myColor.getBlue(),0);
            GL11.glVertex3f(position.x, position.y, position.z);
        }
         
    }
    /**
     * Checks if the particle is alive.
     * @return True if it is alive, false if not.
     */
    public boolean isParticleAlive()
    {
        return alive;
    }
}
